package com.cat.slidingWindow;

/**
 * @author cat
 * @description https://leetcode.cn/problems/binary-subarrays-with-sum/description/
 * @create 2025/8/1 09:25
 * @since JDK17
 */

public class Solution23 {
    public int numSubarraysWithSum(int[] nums, int goal) {
        return numSubarraysWithSum(nums, goal - 1) - numSubarraysWithSum(nums, goal);
    }

    public int f(int[] nums, int k) {
        int ans = 0, n = nums.length, sum = 0;
        for (int l = 0, r = 0; r < n; r++) {
            sum += nums[r];
            while (sum > k) {
                ans += n - r;
                sum -= nums[l--];
            }
        }
        return ans;
    }
}
